package Sorting;

/**
 * Created with IntelliJ IDEA.
 * User: Bjarke
 * Date: 22-10-13
 * Time: 15:19
 * To change this template use File | Settings | File Templates.
 */
public class ArrayStructures {

    private int[] theArray = new int[15];

    private int arraySize = 10;

    public void createRandomArray(){

        for (int i= 0; i < arraySize; i++){

            theArray[i] = (int)(Math.random()*10)+10;

        }
    }

    public void printArray(){
        System.out.println("------------");
        for (int i = 0; i < arraySize; i++){

            System.out.print("| " + i + " | ");
            System.out.println(theArray[i] + " |");
            System.out.println("------------");
        }
    }

    public int getValueAtIndex(int index){
        if (index < arraySize)
            return theArray[index];
        else return 0;

        /*
        for (int i = 0; i < arraySize; i++){

            if (index == i){
                System.out.println(" Index: " + i + " value: " + theArray[i]);
            }

        }
        */
    }

    public boolean doesArrayContainValue(int value){

        boolean valueInArray = false;

        for (int i = 0; i < arraySize; i++)
        if (value == theArray[i]){
            valueInArray = true;
        }
        return valueInArray;
    }

    public int getIndexForValue(int value){

        int v = 0;
        for (int i = 0; i < arraySize; i++){
            if (value == theArray[i])
                v = i;
        }
        return v;
    }

    public void deleteIndex(int index){

        if (index < arraySize){

            for (int i = index; i < (arraySize - 1); i++){

                theArray[i] = theArray[i+1];

            }
            arraySize--;
        }
    }

    public void insertValue(int value){

        if (arraySize < 50){

            theArray[arraySize] = value;

            arraySize++;
        }
    }


    public String linearSearchForValue(int value){

        boolean valueInArray = false;
        String indexesWithValue = "";

        System.out.print("The value was found in the following indexes: ");

        for (int i = 0; i < arraySize; i++){

            if (theArray[i] == value){

                valueInArray = true;
                System.out.print(i + " ");

                indexesWithValue += i + " ";
            }
        }

        if (!valueInArray){

            indexesWithValue = "none";
            System.out.println(indexesWithValue);
        }
        System.out.println();
        return indexesWithValue;
    }

    public void bubbleSort(){

        for (int i = arraySize - 1; i > 1; i--) {

            for (int j = 0; j < i; j++){

                if (theArray[j] > theArray[j+1]){
                    swapValues(j, j+1);
                    printHorzArray(i, j);
                }
                //printHorzArray(i, j);
            }
        }
    }

    public void swapValues(int indexOne, int indexTwo){

        int temp = theArray[indexOne];
        theArray[indexOne] = theArray[indexTwo];
        theArray[indexTwo] = temp;
    }

    public void binarySearchForValue(int value){

        int lowIndex = 0;
        int highIndex = arraySize -1;
        boolean resultFound = false;

        while (lowIndex <= highIndex){

            int middleIndex = (lowIndex + highIndex) / 2;

            if (theArray[middleIndex] < value){
                lowIndex = middleIndex+1;
            }
            else if (theArray[middleIndex] > value){
                highIndex = middleIndex-1;
            }
            else{
                System.out.println("Found a match for value " + value + " at index: " + middleIndex);
                lowIndex = highIndex + 1;
                resultFound = true;
            }
            printHorzArray(middleIndex, -1);
        }

        if (!resultFound){
            System.out.println("No match was found");
        }
    }

    public void selectionSort(){

        for (int x = 0; x < arraySize; x++){

            int minimum = x;
            for (int y = x; y < arraySize; y++){

                if (theArray[minimum] > theArray[y]){

                    minimum = y;
                }
            }
            swapValues(x, minimum);

            printHorzArray(x, -1);
        }
    }

    // not working properly yet
    public void insertionSort(){

        for (int i = 1; i < arraySize; i++){

            int j = 1;
            int toInsert = theArray[i];

            while ((j > 0) && (theArray[j-1] > toInsert)){

                theArray[j] = theArray[j-1];
                j--;
                printHorzArray(i, j);
            }

            theArray[j] = toInsert;
            printHorzArray(i, j);

            System.out.println("\nArray[i]: " + theArray[i] + " Array[j]: " + theArray[j]
            + " toInsert: " + toInsert);
        }
    }

    public void printHorzArray(int i, int j){

        for(int n = 0; n < 51; n++)System.out.print("-");

        System.out.println();

        for(int n = 0; n < arraySize; n++){

            System.out.print("| " + n + "  ");

        }

        System.out.println("|");

        for(int n = 0; n < 51; n++)System.out.print("-");

        System.out.println();

        for(int n = 0; n < arraySize; n++){

            System.out.print("| " + theArray[n] + " ");

        }

        System.out.println("|");

        for(int n = 0; n < 51; n++)System.out.print("-");

        System.out.println();

        // END OF FIRST PART


        // ADDED FOR BUBBLE SORT

        if(j != -1){

            // ADD THE +2 TO FIX SPACING

            for(int k = 0; k < ((j*5)+2); k++)System.out.print(" ");

            System.out.print(j);

        }


        // THEN ADD THIS CODE

        if(i != -1){

            // ADD THE -1 TO FIX SPACING

            for(int l = 0; l < (5*(i - j)-1); l++)System.out.print(" ");

            System.out.print(i);

        }

        System.out.println();

    }


    public static void main(String[] args){

        ArrayStructures newArrayObj = new ArrayStructures();
        newArrayObj.createRandomArray();

        //newArrayObj.bubbleSort();
        //newArrayObj.binarySearchForValue(11);
        //newArrayObj.selectionSort();
        newArrayObj.insertionSort();

        //newArrayObj.printHorzArray(-1,-1);

        //System.out.println(newArrayObj.getValueAtIndex(4));

        //System.out.println(newArrayObj.doesArrayContainValue(14) + " at index " + newArrayObj.getIndexForValue(14));

        //newArrayObj.deleteIndex(3);
        //newArrayObj.insertValue(12);
        //newArrayObj.printArray();

        //System.out.println(newArrayObj.linearSearchForValue(10));

    }

}
